import { defineStore } from 'pinia'
import useUser from './user'

// 返回值为函数
const useCounter = defineStore('counter', {
  state: () => ({
    counter: 100,
    friends: [
      { id: 100, name: 'zs', age: 18 },
      { id: 101, name: 'ls', age: 19 },
      { id: 102, name: 'ww', age: 20 },
    ],
  }),
  getters: {
    // 1.基本使用
    doubleCounter(state) {
      return state.counter * 2
    },
    // 2. 一个getter引入另一个getter
    doubleCounterOne() {
      // 通过this返回getters
      return this.doubleCounter + 1
    },
    // 3. 返回一个函数
    findFriendById(state) {
      return function (id) {
        let friend = state.friends.find((friend) => friend.id === id)
        return friend
      }
    },
    // 4. getters中使用到别的state中的数据
    showMessage(state) {
      const userStore = useUser()
      return `name: ${userStore.name} - counter: ${state.counter}`
    },
  },
  actions: {
    increment() {
      this.counter++
    },
    // 带参数
    incrementNum(num) {
      this.counter += num
    },
  },
})

// 调用返回store
// const counterStore = useCounter()
export default useCounter
